<?php

namespace Shop\Http\Controllers\Wap;

use App\Helpers\SelfResponse;
use Shop\Http\Controllers\BaseController;
use App\Http\Requests\Cms\MessageSendRequest;
use App\Models\Cms\MessageModel;
use Carbon\Carbon;
use DC\UserCenter\Models\UserBaseModel;
use Illuminate\Http\Request;


class MessageController extends BaseController
{

    private static $_messageModel;

    public function __construct()
    {
        self::$_messageModel = new MessageModel();
    }

    public function index(Request $request)
    {
        $type= $request->input('type','inbox');

        self::$_messageModel = self::$_messageModel->with(['fromUser' => function ($query) {
            $query->select(['member_id', 'user_number', 'real_name']);
        }, 'toUser' => function ($query) {
            $query->select(['member_id', 'user_number', 'real_name']);
        }]);
        if ($type == 'inbox') {
            self::$_messageModel = self::$_messageModel->where('from_uid', 0)
                ->orderBy('is_reply');
        } else {
            self::$_messageModel = self::$_messageModel->where('to_uid', 0)
                ->orderBy('is_reply', 'desc');
        }

        $pageNum = $request->input('page_num',1);
        $list = self::$_messageModel->forPage($pageNum,10)->get();

        return $this->wapRender('message.index',[
            'page_title'=>'消息中心',
            'list'=>$list,
            'type'=>$type,
        ]);

    }

    /**
     * @title   写留言 视图
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function addMessage(Request $request)
    {
        $type= $request->input('type','add');

        return $this->wapRender('message.add',[
            'page_title'=>'消息中心',
            'type'=>$type,
        ]);

    }


    /**
     * @title 留言
     * @param Request $request
     * @return
     */
    public function send(Request $request)
    {
        $user_id = $request->input('user_id');

        $data=[
            'to_uid'=>$request->input('uid'),
            'content'=>$request->input('content'),
        ];
        if($request->input('title')){
            $data['title']=$request->input('title');
        }

        if($request->input('msg_img')){
            $data['msg_img']=$request->input('msg_img');
        }

        $data['from_uid'] = $user_id;

        if(!MessageModel::create($data)){
            return SelfResponse::error('发送失败');
        }
        return SelfResponse::success('发送成功','f_Wap_message_index');

    }


    /**
     * 留言删除
     * @param Request $request
     * @return array
     */
    public function deleteMessage(Request $request)
    {

        $user_id = $request->input('user_id');
        //获取要删除的留言信息
        $id = $request->input("message_id");

        $type = $request->input("type");

        $res = self::doDelete($user_id,$id,$type);

        if ($res == 1) {
            return SelfResponse::success('删除成功','f_Wap_message_index');
        }

        return SelfResponse::error("删除失败！");

    }

    public function doDelete($uid,$id,$type)
    {
        switch($type) {
            case 'inbox':
                //作为接收方删除
                $data['status'] = 0;
                if (is_array($id)) {
                    $res = MessageModel::whereIn("id",$id)->where('to_uid',$uid)->update($data);
                    if (!$res) {
                        return 0;
                    }
                    MessageModel::whereIn("id",$id)->where('to_uid',$uid)->where([['status','=',0]])->delete();
                    return 1;
                }
                $res = MessageModel::where("id",$id)->where('to_uid',$uid)->update($data);
                if (!$res) {
                    return 0;
                }
                MessageModel::where("id",$id)->where('to_uid',$uid)->where([['status','=',0]])->delete();
                return 1;
                break;
            case 'outbox':
                //作为发送发删除
                $data['status'] = 0;
                if (is_array($id)) {
                    $res = MessageModel::whereIn("id",$id)->where('from_uid',$uid)->update($data);
                    if (!$res) {
                        return 0;
                    }
                    MessageModel::whereIn("id",$id)->where('from_uid',$uid)->where([['status','=',0]])->delete();
                    return 1;
                }
                $res = MessageModel::where("id",$id)->where('from_uid',$uid)->update($data);
                if (!$res) {
                    return 0;
                }
                MessageModel::where("id",$id)->where('from_uid',$uid)->where([['status','=',0]])->delete();
                return 1;
                break;
            default:
                $data['status'] = 0;
                if (is_array($id)) {
                    $res = MessageModel::whereIn("id",$id)->where('to_uid',$uid)->update($data);
                    if (!$res) {
                        return 0;
                    }
                    MessageModel::whereIn("id",$id)->where('to_uid',$uid)->where([['status','=',0]])->delete();
                    return 1;
                }
                $res = MessageModel::where("id",$id)->where('to_uid',$uid)->update($data);
                if (!$res) {
                    return 0;
                }
                MessageModel::where("id",$id)->where('to_uid',$uid)->where([['status','=',0]])->delete();
                return 1;
        }

    }


    /**
     * @title 回复留言
     * @param Request $request
     * @param $id
     * @return SelfResponse|View
     */
    public function reply(Request $request)
    {
        if($request->isMethod('post')){
            $id = $request->post('id');
            $reply = $request->post('content');
            $info = MessageModel::find($id);
            $info->is_reply = 1;
            $info->is_read = 1;
            $info->reply = $reply;
            $info->replied_at = Carbon::now()->getTimestamp();
            if(!$info->save()){
                return SelfResponse::error('留言回复失败!');
            }
            return SelfResponse::success('留言回复成功','f_Wap_message_index');
        }
    }


}